<template>
  <div
    :style="{paddingBottom:(($store.state.player.showPlayerUi&&!$store.state.player.miniUi)?'64px':'0px')}"
    class="search-box"
  >
    <van-sticky :offset-top="0">
      <div class="search-header">
        <span @click="onBack" class="header-icon-back" tag="div">
          《《《
        </span>
        <input
          @keyup.13="onConfirm"
          v-model="inputModel"
          class="header-input"
          type="text"
          placeholder="输入..."
        >
        <van-icon v-if="inputModel" @click="onClearInput" class="header-icon-closed" name="cross" />
      </div>
    </van-sticky>
    <div class="search-real">
      <!-- 历史搜索 -->
      <div v-if="historyList.length>0" class="history-class">
        <h4>历史搜索</h4>
        <ul class="history-box">
          <li v-for="(item, index) in historyList" :key="index" @click="onClick(item)">
            {{ item }}
          </li>
        </ul>
      </div>
      <!-- 热门搜索 -->
      <div v-if="hotSearchList.length>0" class="hot-class">
        <h4>热搜榜</h4>
        <ul class="list-box">
          <li v-for="(item, index) in hotSearchList" :key="index" @click="onClick(item.searchWord)" class="item-box">
            <p class="hot-index">
              {{ index+1 }}
            </p>
            <p class="hot-title">
              {{ item.searchWord }}
            </p>
            <p class="hot-content">
              {{ item.content }}
            </p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { clearLastSearchPlayList } from '@/utils/localStorage'

export default {
  data () {
    return {
      cssPaddingBottom: false,
      searchEndUi: false,
      inputModel: '' // input 双向绑定
    }
  },
  computed: {
    historyList () {
      return this.$store.state.search.historyList
    },
    hotSearchList () {
      return this.$store.state.search.hotSearchList
    }
  },
  mounted () {
    // 手动触发一次获取历史搜索
    this.$store.commit('search/setHistoryList', '')
    document.documentElement.scrollTop = 0
  },
  beforeDestroy () {
    // 手动清除跳转playlist记录
    clearLastSearchPlayList()
    document.documentElement.scrollTop = 0
  },
  methods: {
    onClearInput () {
      // 清空输入
      this.inputModel = ''
    },
    onBack () {
      this.$store.commit('search/setShowSearchPanel', false)
    },
    onClick (word) {
      // 点击热搜item
      this.inputModel = word
      this.onConfirm()
    },
    onConfirm () {
      if (!this.inputModel) {
        return
      }
      // 跳转到搜索结果页
      this.$router.push({ path: `/search`, query: { word: this.inputModel } })
      // 添加搜索记录
      this.$store.commit('search/setHistoryList', this.inputModel)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/style/global.scss";

.search-box {
  width: 100%;
  height: 100%;
  .search-header {
    width: 100%;
    height: 48px;
    padding-bottom: 2px;
    background: #fff;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .header-input {
      flex: 1;
      height: 38px;
      margin: auto 16px;
      padding-left: 6px;
      outline: none;
      border: none;
      border-bottom: 1px solid rgba(0,0,0,.1);
    }
    .header-icon-closed {
      width: 36px;
      height: 36px;
      line-height: 36px;
      text-align: center;
    }
  }
  .search-real {
    .history-class {
      padding: 6px;
      box-sizing: border-box;
      h4 {
        font-size: 14px;
        font-weight: 600;
      }
      .history-box {
        width: 100%;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        overflow-x: auto;
        li {
          min-width: calc(33.3% - 42px);
          line-height: 24px;
          text-align: center;
          overflow: hidden;
          padding: 0 6px;
          margin-top: 6px;
          margin-left: 6px;
          background: rgba(0,0,0,.05);
          border-radius: 10px;
          font-size: 14px;
          @include ellipsis;
        }
      }
    }
    .hot-class {
      margin: 6px;
      h4 {
        font-size: 14px;
        font-weight: 600;
      }
      .list-box {
        width: 100%;
        padding-top: 6px;
        .item-box:hover {
          opacity: .8;
        }
        .item-box {
          width: 100%;
          padding-left: 36px;
          box-sizing: border-box;
          position: relative;
          padding-bottom: 6px;
          .hot-index {
            position: absolute;
            top: 8px;
            left: 18px;
            font-size: 18px;
            color: #666;
          }
          .hot-title {
            margin-left: 18px;
          }
          .hot-content {
            margin-left: 18px;
            font-size: 12px;
            color: #666;
          }
        }
      }
    }
  }
}
</style>
